<?php
// +----------------------------------------------------------------------
// | Jqcms [ Avenue to Jane ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://jqcms.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: hyacinth <845077398@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Common\Controller\BaseController;

class TagController extends BaseController {
    
    //列表
    public function index(){
       
        $tag = M('Tag');
        if(IS_POST && I('keywords')!=null){
           $where['name'] = array('like','%'.I('keywords').'%');
        }else{
           $where = "";
        }
        $count = $tag->where($where)->count();
        $p = getpage($count,10);
        $list = $tag->field(true)->where($where)->order('id desc')->limit($p->firstRow, $p->listRows)->select();
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $p->show());
        $this->display();
    }
    
    //添加
    public function create(){
        
        $tag= D('Tag');
        
        if(IS_POST){
            if($tag->create()){
                $tag->add();
                $this->success('添加成功!','index');
            }else{
               $this->error($tag->getError()); 
            } 
        }else{
            $this->display();
        }
           
    }
    //编辑
    public function edit($id){
        
        $tag = D('Tag');
        
        if (IS_POST) {
            if($tag->create()){
                $tag->save();
                $this->success('编辑成功!','index');
            }else{
                $this->error($tag->getError()); 
            } 
        }else {
            $data = $tag->find($id);
            $this->assign('data',$data);
            $this->display(); 
        }
        
    }
    
    //单删 
    public function del($id){
    
        $tag = M('Tag');
        $del = $tag->delete($id);
        
        if($del !==0 && $del !==false){
            $this->success('删除成功!');
        }else {
            $this->error('删除失败!');
        }
         
    }
    
    //多删
    public function delselect(){
        
        $tag = M('Tag');
        $idarr = I('id');
        $idstr = implode(',',$idarr);
        $del = $tag->delete($idstr);
        
        if($del !==0 && $del !==false){
            $this->success('删除成功!');
        }else {
            $this->error('删除失败!');
        }
         
    }
    //
  
    
}